﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class App_Common_UserControls_AddToCart : System.Web.UI.UserControl
{
    public string ItemNumber
    {
        get
        {
            return (string)ViewState["ItemNumber"];
        }
        set
        {
            ViewState["ItemNumber"] = value;
        }
    }

    public string ProductTitle
    {
        get
        {
            return (string)ViewState["ProductTitle"];
        }
        set
        {
            ViewState["ProductTitle"] = value;
        }
    }

    public decimal UnitPrice
    {
        get
        {
            return (decimal)ViewState["UnitPrice"];
        }
        set
        {
            ViewState["UnitPrice"] = value;
        }
    }

    private Asah.SiteWorx.Commerce.ShoppingCart MyShoppingCart
    {
        get
        {
            if (Session["MyCart"] == null)
                Session["MyCart"] = new Asah.SiteWorx.Commerce.ShoppingCart();
            return (Asah.SiteWorx.Commerce.ShoppingCart)Session["MyCart"];
        }
        set
        {
            Session["MyCart"] = value;
        }
    }
    
    protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        AddToCart();
    }

    private void AddToCart()
    {
        int quantity = Convert.ToInt32(txtQuantity.Text);
        MyShoppingCart.AddToCart(ItemNumber, ProductTitle, quantity, UnitPrice);
        txtQuantity.Text = "1";

        pnlCartDetails.Visible = true;

        if (quantity > 1)
            lblNumberOfItems.Text = quantity + " items added to your";
        else
            lblNumberOfItems.Text = "1 item added to your";

        lblSubtotal.Text = MyShoppingCart.GetCartTotal().ToString("c");
        lblItemTotal.Text = MyShoppingCart.GetCartQuantity().ToString();

    }
  
}
